package widget;

import javax.swing.*;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.TableCellEditor;

import entity.*;
import form.Form;

import java.awt.*;
import java.util.ArrayList;
import java.util.EventObject;

/**
 * Created by LLAP on 2017/12/25.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class DataTable extends JPanel {
    private Entity entity;
    private JTable table;
    private JButton btnAdd, btnRemove, btnSearch, btnRefresh;
    private JToolBar topbar;
    private JScrollPane dataScrollPane;

    public DataTable() {
        table = new JTable();
        this.add(table);
    }
    public DataTable(Entity entity) {
        this(entity, true);
    }
    public DataTable(Entity entity, boolean toolbar) {
        super(new BorderLayout(5, 5));
        this.entity = entity;
        table = new JTable();
        table.setModel(entity);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        dataScrollPane = new JScrollPane(table);

        if (toolbar) {
            btnRefresh = new JButton("刷新");
            btnAdd = new JButton("添加条目");
            btnRemove = new JButton("删除选中条目");
            btnSearch = new JButton("搜索");

//        topbar = new JPanel(new GridLayout(1, 3, 5, 5));
            topbar = new JToolBar();
            topbar.add(btnRefresh);
            topbar.add(btnAdd);
            topbar.add(btnRemove);
            topbar.add(btnSearch);
            topbar.add(new JLabel("共" + entity.getRowCount() + "条记录"));
            topbar.setRollover(false);
            topbar.setFloatable(false);

            btnRefresh.addActionListener(e -> {
                entity.refresh();
                updateUI();
            });
            btnRemove.addActionListener(e -> {
                try {
                    if (table.getSelectedRows().length > 0) {
                        int[] selRowIndexes = table.getSelectedRows();
                        Object[] selKeys = new Object[selRowIndexes.length];
                        for (int i = 0; i < selRowIndexes.length; i++) {
                            selKeys[i] = table.getValueAt(selRowIndexes[i], 0);
//                        System.out.println(selKeys[i]);
                        }
                        int result = entity.removeData(selKeys);
                        if (result == selKeys.length) {
                            for (int i : selRowIndexes) {
                                entity.removeRow(table.getSelectedRow());
                            }
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            });
            btnAdd.addActionListener(e -> {
                entity.add();
//            entity.refresh();
            });
            btnSearch.addActionListener(e -> {
                entity.search();
            });
            TableCellEditor cellEditor = table.getDefaultEditor(JTable.class);
            cellEditor.addCellEditorListener(new CellEditorListener() {
                @Override
                public void editingStopped(ChangeEvent e) {
                    ArrayList<Integer> index = entity.getEditedIndex();
                    for (int i : index) {
                        Object[] data = new Object[entity.getColumns().length];
                        for (int j = 0; j < data.length; j++) data[j] = table.getValueAt(i, j);
                        entity.updateData(data);
                    }
                }

                @Override
                public void editingCanceled(ChangeEvent e) {

                }
            });
            this.add("North", topbar);
        }
        this.add("Center", dataScrollPane);

    }

    public Object[] getSelectedRow() {
        int i = table.getSelectedRow();
        if (i < 0) return new Object[0];
        Object[] data = new Object[entity.getColumns().length];
        for (int j = 0; j < data.length; j++) data[j] = table.getValueAt(i, j);
        return data;
    }

}
